/*
 * @(#)XfslEndCondition.java        1.0 2000/05/09
 *
 * This file is part of Xfuzzy 3.0, a design environment for fuzzy logic
 * based systems.
 *
 * (c) 2000 IMSE-CNM. The authors may be contacted by the email address:
 *                    xfuzzy-team@imse.cnm.es
 *
 * Xfuzzy is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * Xfuzzy is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

package xfuzzy.xfsl.view;

import xfuzzy.util.*;
import xfuzzy.xfsl.model.XfslEndCondition;
import xfuzzy.xfsl.view.listener.*;

import javax.swing.*;
import java.awt.*;

/**
 * Di�logo para seleccionar las condiciones de t�rmino del proceso
 * de aprendizaje
 * 
 * @author Francisco Jos� Moreno Velo
 *
 */
public class XfslEndDialog extends JDialog {

	//----------------------------------------------------------------------------//
	//                            COSTANTES PRIVADAS                              //
	//----------------------------------------------------------------------------//

	/**
	 * C�digo asociado a la clase serializable
	 */
	private static final long serialVersionUID = 95505666603066L;

	/**
	 * Condici�n no seleccionada 
	 */
	private static final int NOT_SELECTED = -1;

	//----------------------------------------------------------------------------//
	//                            MIEMBROS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Referencia al objeto que almacena las condiciones de t�rmino
	 */
	private XfslEndCondition condition;
	
	/**
	 * Componentes gr�ficos de introducci�n de valores de las condiciones
	 */
	private XTextForm text[] = new XTextForm[9];
	
	/**
	 * Flag que indica que se ha realizado la selecci�n
	 */
	private boolean selected;

	//----------------------------------------------------------------------------//
	//                                CONSTRUCTOR                                 //
	//----------------------------------------------------------------------------//

	/**
	 * Constructor
	 */
	public XfslEndDialog(JFrame frame, XfslEndCondition condition) { 
		super((Frame) frame, "Xfsl", true);
		this.condition = condition;
		this.selected = false;
		build(frame);
	}

	//----------------------------------------------------------------------------//
	//                             M�TODOS P�BLICOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Verifica que se acepte la selecci�n
	 */
	public boolean isSelected() {
		return selected;
	}

	/**
	 * Actualiza los datos con los valores de la ventana
	 */
	public void set() {
		if(!get()) XDialog.showMessage(null,"Values introduced are not correct");
		else { selected = true; setVisible(false); }
	}

	/**
	 * Borra el contenido de las condiciones de finalizaci�n
	 */
	public void unset() {
		for(int i=0; i<9; i++) condition.setLimit(i, NOT_SELECTED);
		selected = true;
		setVisible(false);
	}

	/**
	 * Cierra el di�logo sin provocar cambios en los datos
	 */
	public void cancel() {
		selected = false;
		setVisible(false);
	}
	
	//----------------------------------------------------------------------------//
	//                             M�TODOS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Construye el di�logo de introducci�n de datos
	 */
	private void build(JFrame frame) {
		String label[] = {"Limit on Iterations",
				"Limit on Training Error", "Limit on Training RMSE",
				"Limit on Training MxAE", "Limit on Training Rel. Var.",
				"Limit on Test Error", "Limit on Test RMSE",
				"Limit on Test MxAE", "Limit on Test Rel. Var."};

		for(int i=0; i<text.length; i++) text[i] = new XTextForm(label[i]);
		XTextForm.setWidth(text);
		if(condition.getLimit(0) == NOT_SELECTED) text[0].setText("");
		else text[0].setText(""+ (long) condition.getLimit(0));
		for(int i=1; i<text.length; i++)
			if(condition.getLimit(i) == NOT_SELECTED) text[i].setText("");
			else text[i].setText(""+condition.getLimit(i));

		String lb[] = { "Set", "Unset", "Cancel" };
		XCommandForm form = new XCommandForm(lb,lb, new XfslEndActionListener(this) );
		form.setCommandWidth(100);

		Container content = getContentPane();
		content.setLayout(new BoxLayout(content,BoxLayout.Y_AXIS));
		content.add(new XLabel("End Conditions"));
		for(int i=0; i<text.length; i++) content.add(text[i]);
		content.add(form);

		Point loc = frame.getLocationOnScreen();
		loc.x += 40; loc.y += 200;
		setLocation(loc);
		pack();
	} 

	/**
	 * Obtiene los datos del di�logo
	 */
	private boolean get() {
		boolean good = true;
		for(int i=0; i<9; i++) {
			if(text[i].getText().trim().length() > 0) {
				try {
					if(i==0) condition.setLimit(i, (double) Long.parseLong(text[i].getText()));
					else condition.setLimit(i, Double.parseDouble(text[i].getText()));
				}
				catch (NumberFormatException e) {
					good = false;
					condition.setLimit(i, NOT_SELECTED);
					text[i].setText("");
				}
			}
			else condition.setLimit(i, NOT_SELECTED);
		}
		return good;
	}

}

